package com.guxingke.typecho4j.widget.provider;

import java.util.List;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.User;
import com.guxingke.typecho4j.util.DateUtil;
import com.guxingke.typecho4j.util.MD5Util;

/**
 * 抽象用户组件，封装一些关于用户的操作
 * 
 * @author guxingke
 * */
public abstract class UserBaseWidget extends ProviderWidget {

	/**
	 * 更新用户的最后活动时间
	 * */
	protected boolean updateActivated(User user) {
		user.set("activated", DateUtil.getUnixTimestamp());
		return user.update();
	}

	/**
	 * 更新用户的最后登录时间
	 * */
	protected boolean updateLogged(User user) {
		user.set("logged", DateUtil.getUnixTimestamp());
		return user.update();
	}

	/**
	 * 用户登录
	 * 
	 * @throws Typecho4jException
	 * */
	protected User login(String name, String password)
			throws Typecho4jException {
		User user;
		String temp;
		try {
			temp = MD5Util.EncoderPwdByMd5(password);
		} catch (Exception e) {
			throw new Typecho4jException(700,
					"login has some error");
		}
		user = User.dao.login(name, temp);
		return user;
	}

	protected List<User> getUsers() {
		return User.dao.getUsers();
	}
}
